//
//  WBEmoticonPageView.swift
//  weibo
//
//  Created by  郭航 on 16/7/4.
//  Copyright © 2016年 郭航. All rights reserved.
//

import UIKit

private let cellId = "cellId"

class WBEmoticonPageView: UICollectionView {

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        backgroundColor = UIColor.clearColor()
        
        let layOut = collectionViewLayout as! UICollectionViewFlowLayout
        
        layOut.itemSize = CGSize(width: SCREENW, height: 216 - 37)
        layOut.minimumLineSpacing = 0
        layOut.minimumInteritemSpacing = 0
        
        layOut.scrollDirection = .Horizontal
        
        pagingEnabled = true
        
        showsHorizontalScrollIndicator = false
        showsVerticalScrollIndicator = false
        
        bounces = false
        
        registerClass(WBEmoticonPageCell.self, forCellWithReuseIdentifier: cellId)
        
        dataSource = self
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension WBEmoticonPageView: UICollectionViewDataSource {
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return WBEmoticonsBundle.shareEmoticonsBundle.emoticons.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return WBEmoticonsBundle.shareEmoticonsBundle.emoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath) as! WBEmoticonPageCell
        
        cell.index = indexPath
        cell.emoticons = WBEmoticonsBundle.shareEmoticonsBundle.emoticons[indexPath.section][indexPath.item]
        
        return cell
    }
    
}
